//
// Created by overlord on 18-4-7.
//


#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>
#define LOG_TAG  "TAG"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
JNIEXPORT void JNICALL
Java_com_sat_ndk_JNI_callbackVoid(JNIEnv *env, jobject instance) {
    LOGD("进入了C");
    //1.获取字节码文件
    jclass claz = (*env)->FindClass(env, "com/sat/ndk/JNI");
    LOGD("反射1");
    //2.获取方法名
    //jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
    jmethodID methodID = (*env)->GetMethodID(env, claz, "helloFromJava", "()V");
    LOGD("反射2");
    //3.获取对象
    //4.调用方法
    (*env)->CallVoidMethod(env,instance,methodID);
    LOGD("反射4");
}

JNIEXPORT void JNICALL
Java_com_sat_ndk_JNI_callbackVoidInt(JNIEnv *env, jobject instance) {

    //1.获取字节码文件
    jclass claz = (*env)->FindClass(env, "com/sat/ndk/JNI");
    //2.获取方法名
    //jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
    jmethodID methodID = (*env)->GetMethodID(env, claz, "add", "(II)I");
    //3.获取对象
    //4.调用方法
    jint result = (*env)->CallIntMethod(env, instance, methodID, 6, 8);
    LOGD("Add结果=%d", result);
}

JNIEXPORT void JNICALL
Java_com_sat_ndk_JNI_callbackVoidString(JNIEnv *env, jobject instance) {

    jclass claz = (*env)->FindClass(env, "com/sat/ndk/JNI");

    jmethodID methodID = (*env)->GetMethodID(env,claz, "printString", "(Ljava/lang/String;)V");

    jstring s = (*env)->NewStringUTF(env, "我日你妈");

    (*env)->CallVoidMethod(env,instance,methodID,s);
}

JNIEXPORT void JNICALL
Java_com_sat_ndk_JNI_callbackVoidToast(JNIEnv *env, jobject instance) {
    LOGD("进入C");
    jclass claz = (*env)->FindClass(env, "com/sat/ndk/JNI");

    jmethodID methodID = (*env)->GetMethodID(env, claz, "showToast", "(Ljava/lang/String;)V");

    //创建反射的类的对象
     //jobject object = (*env)->AllocObject(env, claz);

    jstring s = (*env)->NewStringUTF(env, "我日你妈");
    (*env)->CallVoidMethod(env,instance,methodID,s);
}
